package com.cytech.cloudclinic.moudle.main.clinic.admission.diagnosis;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.ActionChildAdapter;
import com.cytech.cloudclinic.adapter.ActionMenuAdapter;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.bean.Action;
import com.cytech.cloudclinic.bean.event.RefreshAction;
import com.cytech.cloudclinic.bean.event.UpdateAddTagEvent;
import com.cytech.cloudclinic.bean.req.AddPrescription;
import com.cytech.cloudclinic.bean.req.ReqDiagnosis;
import com.cytech.cloudclinic.dialog.AddActionDialog;
import com.cytech.cloudclinic.dialog.AddDrugTagDialog;
import com.cytech.cloudclinic.moudle.main.clinic.admission.AdmissionStyleFragment;
import com.cytech.cloudclinic.moudle.main.drug.add.AddDrugActivity;
import com.yechaoa.yutils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.cytech.cloudclinic.app.Constant.MODE_MIN_PACKAGE_ID;

/**
 * 主诉与诊断
 */
public class DiagnosisFragment extends BaseFragment<DiagnosisPresenter> implements DiagnosisView {
    @BindView(R.id.rv_category_now_history)
    RecyclerView menuRv;
    @BindView(R.id.rv_now_history)
    RecyclerView contentRv;
    @BindView(R.id.et_now_history)
    EditText nowHistoryEt;
    @BindView(R.id.et_diagnosis)
    EditText diagnosisEt;
    @BindView(R.id.et_previous_history)
    EditText previousHistoryEt;
    @BindView(R.id.et_allergic_history)
    EditText allergicHistoryEt;
    @BindView(R.id.tv_action_type)
    TextView typeTv;

    private AddActionDialog actionDialog;
    private int drugTagId = 0;
    private int parentId = 0;
    // 1 主诉 2 诊断 3 既往史 4 过敏史
    private int type = 1;
    private List<Action> mMenuActionList;
    private List<Action> mActionContentList;

    private ActionMenuAdapter menuAdapter;
    private ActionChildAdapter contentAdapter;

    private ReqDiagnosis reqDiagnosis;

    public static DiagnosisFragment newInstance(int drugTagId) {
        DiagnosisFragment fragment = new DiagnosisFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("drugTagId",drugTagId);
        fragment.setArguments(bundle);
        //如果有参数，使用Bundle传递
        return fragment;
    }

    @Override
    protected DiagnosisPresenter createPresenter() {
        return new DiagnosisPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_addmission_diagnosis;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        nowHistoryEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    typeTv.setText("主诉&现病史");
                    type = 1;
                    presenter.getActionList(buildActionReq(0,true),true);
                }
            }
        });

        diagnosisEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    typeTv.setText("诊断");
                    type = 2;
                    presenter.getActionList(buildActionReq(0,true),true);
                }
            }
        });
    }

    @Override
    protected void initData() {
        mMenuActionList = new ArrayList<>();
        menuAdapter = new ActionMenuAdapter(mMenuActionList);
        menuRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        menuRv.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                parentId = menuAdapter.getItem(position).action_id;
                menuAdapter.setCurrentPos(position);
                presenter.getActionList(buildActionReq(parentId,true),false);
            }
        });

        mActionContentList = new ArrayList<>();
        contentAdapter = new ActionChildAdapter(R.layout.item_add_drug_tag,mActionContentList);
        contentRv.setLayoutManager(new GridLayoutManager(getActivity(),3));
        contentRv.setAdapter(contentAdapter);
        contentAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (contentAdapter.getItem(position).isAddData) {
                    actionDialog = new AddActionDialog(drugTagId, parentId,type);
                    actionDialog.show(getFragmentManager(), "contentRv");
                } else {
                    contentAdapter.setCurrentPos(position);
                    if(type == 1){
                        nowHistoryEt.setText(contentAdapter.getItem(position).value);
                    }else if(type == 2){
                        diagnosisEt.setText(contentAdapter.getItem(position).value);
                    }
                }
            }
        });

        drugTagId = getArguments().getInt("drugTagId");

        presenter.getActionList(buildActionReq(0,true),true);
    }

    @Override
    public void getMenuListSuccess(List<Action> data) {
        if(data!=null&&data.size()>0){
            parentId = data.get(0).action_id;
            presenter.getActionList(buildActionReq(parentId,false),false);
        }

//        Action action = new Action();
//        action.value = "全部";
//        action.action_id = 0;
//        data.add(0,action);
        menuAdapter.setNewInstance(data);
    }

    @Override
    public void getActionContentSuccess(List<Action> data) {
        data.add(new Action(true));
        contentAdapter.setNewInstance(data);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateData(RefreshAction event) {
        presenter.getActionList(buildActionReq(parentId,false),false);
    }

    /**
     *
     * @param parentId  父id,一级分类为0
     * @param isAll 0 不处理 1 为全部 true为显示全部
     * @return
     */
    public ReqDiagnosis buildActionReq(int parentId,boolean isAll){
        if(reqDiagnosis == null){
            reqDiagnosis = new ReqDiagnosis();
        }
        reqDiagnosis._type = type;
        reqDiagnosis.drug_tag_id = drugTagId;
        reqDiagnosis.is_all = isAll?1:0;
        reqDiagnosis.parent_id=parentId;
        return reqDiagnosis;
    }

    @OnClick(R.id.btn_diagnosis_next)
    public void nextClick(View view){
        AddPrescription prescription = new AddPrescription();
        prescription.action = nowHistoryEt.getText().toString();
        prescription.diagnose = diagnosisEt.getText().toString();
        prescription.previous_history =previousHistoryEt.getText().toString();
        prescription.allergic_history =allergicHistoryEt.getText().toString();

        if(TextUtils.isEmpty(prescription.action)){
            ToastUtil.showToast("主诉不能为空");
            return;
        }
        if(TextUtils.isEmpty(prescription.diagnose)){
            ToastUtil.showToast("诊断不能为空");
            return;
        }
      ((AdmissionStyleFragment)getParentFragment()).switchReportFragment(prescription);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
